/**
 * Project Name: mms-provider-user
 * File Name: AccountController
 * Package Name: com.scb.dev.mms.controller
 * Date: 2018/11/27 12:07 PM
 * Copyright (c) 2018, Wang, Haoyue All Rights Reserved.
 */
package com.scb.dev.mms.controller;

import com.scb.dev.mms.common.DataCommon;
import com.scb.dev.mms.common.ResponseResult;
import com.scb.dev.mms.common.ResultType;
import com.scb.dev.mms.common.StatusCode;
import com.scb.dev.mms.dto.AccountFormDTO;
import com.scb.dev.mms.pojo.Account;
import com.scb.dev.mms.service.IAccountService;
import com.scb.dev.mms.vo.LoginVO;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * ClassName: AccountController
 * Description: Account Controller
 * Date: 2018/11/27 12:07 PM
 *
 * @author Wang, Haoyue
 * @version V1.0
 * @since JDK 1.8
 */
@Slf4j
@RestController
@RequestMapping("/sm/account/")
@Api(value = "AccountController", description = "Account API")
public class AccountController {

    @Autowired
    private IAccountService accountService;

    /**
     * addAccount: add Account
     *
     * @param account
     * @return ResponseResult
     */
    @ApiOperation(value = "addAccount", notes = "Use this API to add a new account.")
    @RequestMapping(value = "account", method = RequestMethod.POST)
    public ResponseResult addAccount(@RequestBody @ApiParam(name = "account", value = "account entity") Account account, HttpSession session) {
        log.info("Account Controller -> addAccount 添加账户");
        String accountId = (String) session.getAttribute(DataCommon.CURRENT_ACCOUNT_ID);
        if(StringUtils.isBlank(accountId)){
            return new ResponseResult(false, ResultType.USER_NOT_LOGIN, StatusCode.FAULT_FLAG);
        }
        return accountService.insertAccount(account, accountId);
    }

    /**
     * deleteAccount: delete Account
     *
     * @param accountId
     * @return ResponseResult
     */
    @ApiOperation(value = "deleteAccount", notes = "Use this API to delete a account.")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "accountId", value = "account id", required = true, dataType = "String"),
    })
    @RequestMapping(value = "account/{accountId}", method = RequestMethod.DELETE)
    public ResponseResult deleteAccount(@PathVariable("accountId") String accountId, HttpSession session) {
        log.info("Account Controller -> deleteAccount 删除账户");
        String curr_accountId = (String) session.getAttribute(DataCommon.CURRENT_ACCOUNT_ID);
        if(StringUtils.isBlank(curr_accountId)){
            return new ResponseResult(false, ResultType.USER_NOT_LOGIN, StatusCode.FAULT_FLAG);
        }
        return accountService.deleteAccount(accountId, curr_accountId);
    }

    /**
     * updateAccount: update account
     *
     * @param account
     * @return ResponseResult
     */
    @ApiOperation(value = "updateAccount", notes = "Use this API to update a account.")
    @RequestMapping(value = "account", method = RequestMethod.PUT)
    public ResponseResult updateAccount(@RequestBody @ApiParam(name = "account", value = "account entity") Account account, HttpSession session) {
        log.info("Account Controller -> updateAccount 更新账户信息");
        String accountId = (String) session.getAttribute(DataCommon.CURRENT_ACCOUNT_ID);
        if(StringUtils.isBlank(accountId)){
            return new ResponseResult(false, ResultType.USER_NOT_LOGIN, StatusCode.FAULT_FLAG);
        }
        return accountService.updateAccount(account, accountId);
    }

    /**
     * findOne: get account info by accountId
     *
     * @param accountId
     * @return ResponseResult
     */
    @ApiOperation(value = "findOne", notes = "Use this API to find a account by id.")
    @ApiImplicitParam(paramType = "path", name = "accountId", value = "account id", required = true, dataType = "String")
    @RequestMapping(value = "account/{accountId}",method = RequestMethod.GET)
    public ResponseResult findOne(@PathVariable("accountId") String accountId, HttpSession session) {
        log.info("Account Controller -> findOne 查询一个账户");
        String curr_accountId = (String) session.getAttribute(DataCommon.CURRENT_ACCOUNT_ID);
        if(StringUtils.isBlank(curr_accountId)){
            return new ResponseResult(false, ResultType.USER_NOT_LOGIN, StatusCode.FAULT_FLAG);
        }
        return accountService.queryOne(accountId);
    }

    /**
     * findAll: get all accounts
     *
     * @return ResponseResult
     */
    @ApiOperation(value = "findAll", notes = "Use this API to find all accounts' details.")
    @RequestMapping(value = "account", method = RequestMethod.GET)
    public ResponseResult findAll(HttpSession session) {
        log.info("Account Controller -> findAll 查询所有账户");
        String accountId = (String) session.getAttribute(DataCommon.CURRENT_ACCOUNT_ID);
        if(StringUtils.isBlank(accountId)){
            return new ResponseResult(false, ResultType.USER_NOT_LOGIN, StatusCode.FAULT_FLAG);
        }
        return accountService.queryAll();
    }

    /**
     * accountLogin: account login
     *
     * @param accountFormDTO
     * @return ResponseResult
     */
    @ApiOperation(value = "accountLogin", notes = "Use this API to login.")
    @RequestMapping(value="login", method={RequestMethod.POST})
    public ResponseResult accountLogin(@RequestBody AccountFormDTO accountFormDTO, HttpSession session) {
        log.info("Account Controller -> accountLogin 登录");
        ResponseResult responseResult = accountService.accountLogin(accountFormDTO);
        if (responseResult.getCode() == 200){
            log.info("put employeeId and accountId into session");
            session.setAttribute(DataCommon.CURRENT_EMPLOYEE_ID, ((LoginVO)responseResult.getData()).getEmployeeId());
            session.setAttribute(DataCommon.CURRENT_ACCOUNT_ID, ((LoginVO)responseResult.getData()).getAccountId());
        }
        return responseResult;
    }
}
